﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Text.RegularExpressions;

namespace Ex1Student
{
    class Program
    {
        static void Main()
        {
            var students = new List<Student>
            {
                new Student("Petur","Subev",121206,"0097876","qr3ca@abv.bg",new List<int>{6,6,6,5,4,4,5,6},1),
                new Student("Marin","Atanasov",325123,"02 151314","min4o@abv.bg",new List<int>{2,3,4,5,3,4,6},3),
                new Student("Petur","Marchev",123123,"088743561","march3v@gmail.com",new List<int>{6,6,6,5,4,4,5,6},1),
                new Student("Georgi","Vasilev",123123,"02/123123","g88v@abv.bg",new List<int>{2,5,5,5,5,3,3,4,2,3},2),
                new Student("Bojidar","Ivanchev",144206,"02 1313","bojko@abv.bg",new List<int>{2,3,4,5,6,3,4,2},4)
            };

            var student2ndGroup =
                from s in students
                where s.GroupNumber == 2
                orderby s.FirstName
                select s;
            Console.WriteLine("From gr 2");
            student2ndGroup.ToList().ForEach(x => Console.WriteLine(x));

            var studentsFrom2ndGRoup = students.Where(x => x.GroupNumber == 2).OrderBy(x => x.FirstName);

            var studentsFromAbv = students.Where(x => x.EMail.EndsWith("abv.bg"));

            var abvStudents =
                from s in students
                where s.EMail.EndsWith("abv.bg")
                select s;
            Console.WriteLine();
            Console.WriteLine("from abv.bg");
            abvStudents.ToList().ForEach(x => Console.WriteLine(x));

            var SofiaNumbersStudents =
                from s in students
                where Regex.IsMatch(s.Tel, @"\b02[\/\s-]\d{6}\b")
                select s;
            Console.WriteLine();
            Console.WriteLine("from Sofia");
            SofiaNumbersStudents.ToList().ForEach(x => Console.WriteLine(x));

            var nerds =
                from s in students
                where s.Marks.Contains(6)
                select new { FullName = string.Format("{0} {1}", s.FirstName, s.LastName), Marks = s.Marks };
            Console.WriteLine();
            Console.WriteLine("Nerds");
            foreach (var nerd in nerds)
            {
                Console.WriteLine(nerd.FullName);
                foreach (var m in nerd.Marks)
                {
                    Console.Write("{0} ", m);
                }
                Console.WriteLine();
            }
            Console.WriteLine();
            Console.WriteLine("Cool kids with 2by2");
            var studentsWith2Marks = students.Where(s => s.Marks.Count(m => m == 2) == 2).
                    Select(s => new { FullName = string.Format("{0} {1}", s.FirstName, s.LastName), Marks = s.Marks });
            foreach (var nerd in studentsWith2Marks)
            {
                Console.WriteLine(nerd.FullName);
                foreach (var m in nerd.Marks)
                {
                    Console.Write("{0} ", m);
                }
                Console.WriteLine();
            }
            Console.WriteLine();
            Console.WriteLine("Marks from 2006");
            var marks = students.Where(x => x.Fn % 100 == 6).SelectMany(x => x.Marks);
            foreach (var m in marks)
            {
                Console.Write("{0} ",m);
            }
            Console.WriteLine();

            var groups = new List<Group>
            {
                new Group(1,"Maths"),
                new Group(2,"Devs"),
                new Group(3,"Testers"),
                new Group(4,"Maths")
            };

            var StudentsFromMath =
                from g in groups
                join s in students
                on g.GroupNumber equals s.GroupNumber
                where g.DepartmentName == "Maths"
                select s;
            Console.WriteLine();
            Console.WriteLine("From Maths:");
            foreach (var m in StudentsFromMath)
            {
                Console.WriteLine("\t{0} ", m);
            }
            Console.WriteLine();
            Console.WriteLine("Grouped By GroupNAME");
            var StudentsGroups =
                from g in groups
                join s in students
                on g.GroupNumber equals s.GroupNumber
                group s by g.DepartmentName
                ;
            foreach (var s in StudentsGroups)
            {
                Console.WriteLine(s.Key);
                foreach (var item in s)
                {
                    Console.WriteLine("\t"+item);
                }
            }
            //extension methods
            Console.WriteLine("grouped by ext");
            var extGrouped = students.Join(groups, s => s.GroupNumber, g => g.GroupNumber, (name, dept) => new { name, dept })
                .GroupBy(m => m.dept.DepartmentName);
            foreach (var s in extGrouped)
            {
                Console.WriteLine(s.Key);
                foreach (var item in s)
                {
                    Console.WriteLine("\t" + item.name);
                }
            }
        }
    }
}
